package cn.tedu.vrd.mapper;

import cn.tedu.vrd.vo.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{author},#{intro}" +
            ",#{url},0,0,#{created},#{categoryId})")
    void insert(Product product);

    @Select("select id,title,author,url,created,view_count,like_count from product")
    @Result(property = "viewCount",column = "view_count")
    @Result(property = "likeCount",column = "like_count")
    List<Product> selectAdmin();

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select id,title,url,view_count,like_count from product")
    @Result(property = "viewCount",column = "view_count")
    @Result(property = "likeCount",column = "like_count")
    List<Product> select();

    @Select("select id,title,author,url from product order by view_count desc limit 0,4")
    List<Product> selectView();

    @Select("select id,title,author,url from product " +
            "order by like_count desc limit 0,4")
    List<Product> selectLike();

    @Select("select id,title,url,view_count,like_count from product where category_id=#{cid}")
    @Result(property = "viewCount",column = "view_count")
    @Result(property = "likeCount",column = "like_count")
    List<Product> selectByCid(int cid);
    //title like "%美女%"                                                #{变量} 如果变量是字符串会自动在变量的两端加上引号
//                                                                       ${变量} 不管是什么类型都不会带引号   存在SQL注入问题
    @Select("select id,title,url,view_count,like_count from product where title like '%${wd}%' ")
    @Result(property = "viewCount",column = "view_count")
    @Result(property = "likeCount",column = "like_count")
    List<Product> selectByWd(String wd);

    @Select("select * from product where id=#{id}")
    @Result(property = "viewCount",column = "view_count")
    @Result(property = "likeCount",column = "like_count")
    Product selectById(int id);

    @Update("update product set like_count = like_count+1 where id = #{id}")
    void updateLikeCount(int id);

    @Update("update product set view_count = view_count+1 where id = #{id}")
    void updateViewCount(int id);

    @Delete("delete from product where category_id = #{id}")
    void deleteByCid(int id);
}
